package android.se1;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class HousingActivity extends Activity {
	EditText genderpreference, rent, comments, address, phonenumber;
	Button submithousing;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork() // or detectAll() for all detectable problems
		.penaltyLog().build());
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.housing);
		
		genderpreference = (EditText) findViewById(R.id.editText1);
		rent = (EditText) findViewById(R.id.editText3);
		comments = (EditText) findViewById(R.id.editText4);
		address = (EditText) findViewById(R.id.editText5);
		phonenumber = (EditText) findViewById(R.id.editText6);
		Button submithousing = (Button) findViewById(R.id.button1);
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		final String email_address=settings.getString("email",null );
		
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		
		submithousing.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub

				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

				String rentCheck = rent.getText().toString();
								
				Pattern pattern = Pattern.compile("[0-9, ]*");
				Matcher matcher = pattern.matcher(rentCheck);
		
				if(!matcher.matches())
				{
					
					//Toast.makeText(SecondActivity.this,"Invalid Name. Please type again.", Toast.LENGTH_LONG).show();
					
					alertDialog.setTitle("Housing Advertisement Post");
					alertDialog
							.setMessage("Invalid Rent. Please type again.");
					alertDialog.setButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int type) {
									// here you can add functions
								}
							});
					alertDialog.show();
				}else{
					postParameters.add(new BasicNameValuePair("genderpreference", genderpreference.getText().toString()));

					postParameters.add(new BasicNameValuePair("rent", rent.getText().toString()));

					postParameters.add(new BasicNameValuePair("comments", comments.getText().toString()));

					postParameters.add(new BasicNameValuePair("address", address.getText().toString()));

					postParameters.add(new BasicNameValuePair("phonenumber", phonenumber.getText().toString()));
					
					postParameters.add(new BasicNameValuePair("email", email_address));

					String response = null;

					try {

						response = CustomHttpClient.executeHttpPost(
								//"http://192.168.1.145/CSE5324/housing_post.php",
								//"http://129.107.149.180/CSE5324/signin.php",
								"http://omega.uta.edu/~nxk0518/CSE5324/housing_post.php",
								//"http://192.168.0.3/mavbulletin/signin.php",
								postParameters);

						String result = response.toString();

						result = result.replaceAll("\\s+", "");

						if (result.equals("1")) {
							System.out.println("Result =" + result);
							//error.setText("Sign In Successful");
							//Toast.makeText(MavBulletinActivity.this,"Sign In Successful.", Toast.LENGTH_LONG).show();
							//System.out.println("Result =" + result);
							//System.out.println("Email =" + email_address);
							//System.out.println("Gender Preference =" + genderpreference.getText().toString());
							//System.out.println("Rent =" + rent.getText().toString());
							
							//Log.e("log_tag", "Email =" + email_address);
							
							alertDialog.setTitle("Housing Advertisement Post");
							alertDialog
									.setMessage("Advertisement Successfully Posted.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int which) {
											// here you can add functions
											Intent viewadsScreen = new Intent(getApplicationContext(), ThirdActivity.class);
											startActivity(viewadsScreen);
										}
									});
							alertDialog.show();
							/*							
							SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
							SharedPreferences.Editor editor = sharedPref.edit();
							editor.putString("email",email_address);
							editor.commit();
							
							Intent viewadsScreen = new Intent(getApplicationContext(),
									ThirdActivity.class);
							startActivity(viewadsScreen);
							*/
						} else {
							System.out.println("Result =" + result);
							//error.setText("Sorry!! Incorrect Email Address or Password");
							alertDialog.setTitle("Housing Advertisement Post");
							alertDialog
									.setMessage("Error in posting advertisement.");
							alertDialog.setButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int type) {
											// here you can add functions
										}
									});
							alertDialog.show();
						}
					} catch (Exception e) {
						Log.e("log_tag",
								"Error in http connection!!" + e.toString());
						//email.setText(e.toString());

					}

				}

			}

		});
	}
}
